﻿Public Class frmGroup_Form 
    Public is_edit As Boolean = False
    Public id As String = ""

    Private Sub loadContact()
        Dim query As String = "SELECT * FROM tb_contact ORDER BY nama_kontak ASC"
        Dim data_temp As DataTable = mysql_query(query)
        Dim data As New DataTable

        data.Columns.Add("id_kontak")
        data.Columns.Add("cek").DataType = GetType(Boolean)
        data.Columns.Add("nama_kontak")
        data.Columns.Add("email")

        For i As Integer = 0 To data_temp.Rows.Count - 1
            data.Rows.Add(data_temp.Rows(i)("id_kontak").ToString, False, data_temp.Rows(i)("nama_kontak").ToString, data_temp.Rows(i)("email").ToString)
        Next

        GridControlList.DataSource = data
    End Sub

    Private Sub frmGroup_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        loadContact()
        If is_edit Then
            Me.Text = "Edit Group"
            If mysql_isconnected() Then
                Dim data As DataTable
                data = mysql_query("SELECT * FROM tb_contact_group WHERE id_grup_kontak='" & id & "'")
                If data.Rows.Count > 0 Then
                    TextEditName.Text = data.Rows(0)("grup_kontak")
                    data = mysql_query("SELECT * FROM tb_contact_group_member WHERE id_grup_kontak='" & id & "'")
                    For i As Integer = 0 To data.Rows.Count - 1
                        For j As Integer = 0 To GridViewList.RowCount - 1
                            If GridViewList.GetRowCellValue(j, "id_kontak").ToString = data.Rows(i)("id_kontak").ToString Then
                                GridViewList.SetRowCellValue(j, "cek", True)
                            End If
                        Next
                    Next

                    If data.Rows.Count = GridViewList.RowCount Then
                        CheckEditAllContact.Checked = True
                    Else
                        CheckEditAllContact.Checked = False
                    End If

                Else
                    MessageBox.Show("Data Not Found. Please Try Again Later")
                    Me.Close()
                End If
            Else
                MessageBox.Show("Cannot Edit Contact Right Now. Please Try Again Later")
                Me.Close()
            End If
        Else
            Me.Text = "Add Group"
        End If
    End Sub

    Private Sub GridViewList_RowClick(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowClickEventArgs) Handles GridViewList.RowClick
        If GridViewList.GetFocusedRowCellValue("cek") = False Then
            GridViewList.SetFocusedRowCellValue("cek", True)

            Dim cek As Boolean = True

            For i As Integer = 0 To GridViewList.RowCount - 1
                If GridViewList.GetRowCellValue(i, "cek") = False Then
                    cek = False
                End If
            Next

            If cek = True Then
                CheckEditAllContact.Checked = True
            End If
        Else
            GridViewList.SetFocusedRowCellValue("cek", False)
            CheckEditAllContact.Checked = False
        End If
    End Sub

    Private Sub SimpleButtonSave_Click(sender As Object, e As EventArgs) Handles SimpleButtonSave.Click
        Dim query As String
        If TextEditName.Text.Equals("") = False Then
            If is_edit = False Then
                query = String.Format("INSERT INTO tb_contact_group(grup_kontak) VALUES('{0}')", TextEditName.Text)
                mysql_non_query(query)

                query = "SELECT LAST_INSERT_ID()"
                id = mysql_query(query, 0)

                For i As Integer = 0 To GridViewList.RowCount - 1
                    If GridViewList.GetRowCellValue(i, "cek") = True Then
                        query = String.Format("INSERT INTO tb_contact_group_member(id_grup_kontak,id_kontak) VALUES('{0}','{1}')", id, GridViewList.GetRowCellValue(i, "id_kontak").ToString)
                        mysql_non_query(query)
                    End If
                Next
                Me.Close()
                frmGroup.loadGroup()
            Else
                mysql_non_query(String.Format("UPDATE tb_contact_group SET grup_kontak = '{0}' WHERE id_grup_kontak = '{1}'", TextEditName.Text, id))

                mysql_non_query(String.Format("DELETE FROM tb_contact_group_member WHERE id_grup_kontak = '{0}'", id))

                For i As Integer = 0 To GridViewList.RowCount - 1
                    If GridViewList.GetRowCellValue(i, "cek") = True Then
                        query = String.Format("INSERT INTO tb_contact_group_member(id_grup_kontak,id_kontak) VALUES('{0}','{1}')", id, GridViewList.GetRowCellValue(i, "id_kontak").ToString)
                        mysql_non_query(query)
                    End If
                Next
                Me.Close()
                frmGroup.loadGroup()
            End If
        Else
            MessageBox.Show("Input all required form")
        End If
    End Sub

    Private Sub CheckEditAllContact_Click(sender As Object, e As EventArgs) Handles CheckEditAllContact.Click
        If CheckEditAllContact.Checked = False Then
            For i As Integer = 0 To GridViewList.RowCount - 1
                GridViewList.SetRowCellValue(i, "cek", True)
            Next
        Else
            For i As Integer = 0 To GridViewList.RowCount - 1
                GridViewList.SetRowCellValue(i, "cek", False)
            Next
        End If
    End Sub


End Class